/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lcduilwuit;

/**
 *
 * @author anup.s
 */
import com.sun.lwuit.Dialog;
import com.sun.lwuit.List;
import java.util.Vector;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.Image;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.Container;
import com.sun.lwuit.Component;

public class Form extends com.sun.lwuit.Form implements Displayable,ListCellRenderer{

    private ItemStateListener iListener;
  //  private Vector items = new Vector();

    public Form(String title) {
        super(title);

    }

    public Form() {
    }
    
//intialization block
    {
        setLayout(lcduilwuit.Display.boxLayout_Y);
        setMenuCellRenderer(this);
    }
    public void addComponent(Component cmp) {
    //    items.addElement(cmp);
        super.addComponent(cmp);

    }

    public void append(Item cmp) {
    //    items.addElement(cmp);
        super.addComponent(cmp);
        cmp.setItemStateListener(iListener);

    }

    public void append(String str) {
        Label cmp = new Label(str);
        cmp.setFocusable(true);
        append(cmp);//.getContainer());
    //    items.addElement(cmp);

    }

    public void append(Image img) {
        Label cmp = new Label(img);
        append(cmp);//.getContainer());
    //    items.addElement(cmp);

    }

    public void deleteAll() {
    //    items.removeAllElements();
        removeAll();
    }

   /* public void insert(int itemNum, Component item) {
        if (itemNum == items.size()) {
            addComponent(item);
        } else if (itemNum < items.size()) {
            addComponent(itemNum, item);
        } else {
            throw new IllegalArgumentException("CAn not insert beyond this");
        }
        items.insertElementAt(item, itemNum);
        show();

    }*/

    public void setItemStateListener(ItemStateListener iListener) {
        this.iListener = iListener;
    }

    public int size() {
        //return items.size();
        return getContentPane().getComponentCount();
    }

    public Item get(int index) {
       // Object item = items.elementAt(index);
          Object item  = getContentPane().getComponentAt(index);
        if (item instanceof Item) {
            return (Item) item;
        } else {
            return new Item() {

                public void setItemStateListener(ItemStateListener il) {
                }

                public String getLabel() {
                    return "";
                }
            };
        }

    /*System.out.println(getComponentAt(index));

    if(getComponentAt(index) instanceof Label)
    {
    System.out.println("Yes");
    }
    else if (getComponentAt(index) instanceof com.sun.lwuit.Label)
    {
    System.out.println("NO"+((com.sun.lwuit.Label)getComponentAt(index)).getText());
    try{
    throw new Exception ();

    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    for(int i=0;i<size();i++)
    {
    mujmail.Logger.log("Item at"+i+" = "+getComponentAt(i));
    }
    return (Item)getComponentAt(index);*/
    }

    public void addCommand(Command cmd) {
      if(cmd==null)
          throw new IllegalArgumentException("Null Command Can not be added");
      super.addCommand(cmd);
    }

    public void delete(int index) {
        try{
            Component itemToDelete = this.getContentPane().getComponentAt(index);
       //Component) items.elementAt(index);
      //  items.removeElementAt(index);
        removeComponent(itemToDelete);
         }
        catch(Exception e)
        {
            
        }
        revalidate();
    }

   /* public void set(int n, Item item) {
        insert(n, item);
        delete(n + 1);
    }*/
    private int textPos;

    public void incrementPosition() {
        textPos -= 5;
    }

    public void resetPosition() {
        textPos = 0;
    }

    protected com.sun.lwuit.List createCommandList(java.util.Vector commands) {
        List l = new List(bubbleSort(commands)){
            private long tickTime = System.currentTimeMillis();
            private int lastSel = -1;
            public boolean animate(){
            boolean val = super.animate();
                if (hasFocus()) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - tickTime > 100) {
                        if (lastSel == getSelectedIndex()) {
                            incrementPosition();
                        } else {
                            lastSel = getSelectedIndex();
                            resetPosition();
                        }
                        tickTime=System.currentTimeMillis();
                        return true;
                    }
                }
                else{
                    System.out.println("NO FOCUS");
                }
                return val;
            }
        };
        l.setUnSelectedStyle(UIManager.getInstance().getComponentStyle("CommandList"));
        l.setSelectedStyle(UIManager.getInstance().getComponentStyle("CommandList"));
        ((Component) l.getRenderer()).setUnSelectedStyle(UIManager.getInstance().getComponentStyle("Command"));
        ((Component) l.getRenderer()).setSelectedStyle(UIManager.getInstance().getComponentSelectedStyle("Command"));
        l.setFixedSelection(List.FIXED_NONE_CYCLIC);
        return l;
        // return super.createCommandList(bubbleSort(commands));
    }

    public static Vector bubbleSort(Vector vct) {
        for (int k = 0; k < vct.size() - 1; k++) {
            boolean isSorted = true;

            for (int i = 1; i < vct.size() - k; i++) {

                if (((Command) vct.elementAt(i)).getPriority() < ((Command) vct.elementAt(i - 1)).getPriority()) {
                    Object tempObj = vct.elementAt(i);
                    vct.setElementAt(vct.elementAt(i - 1), i);
                    vct.setElementAt(tempObj, i - 1);
                    isSorted = false;

                }
            }

            if (isSorted) {
                break;
            }
        }
        return vct;
    }


    public Component getListFocusComponent(List list) {
        return null;
    }
    int dialogWidth;
    protected com.sun.lwuit.Command showMenuDialog(Dialog menu)
    {
       dialogWidth=menu.getWidth();
        return super.showMenuDialog(menu);
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        Container cntr = new Container(lcduilwuit.Display.boxLayout_X);
        com.sun.lwuit.Label num = new com.sun.lwuit.Label("");
        com.sun.lwuit.Label text = new com.sun.lwuit.Label("");
        num.setText("" + (index + 1) + ".");
       num.getStyle().setBgTransparency((byte)0);
       text.getStyle().setBgTransparency((byte)0);
       text.getStyle().setMargin(0, 0, 0,0);
              num.getStyle().setMargin(0, 0, 0,0);

        text.setText(value.toString());
        cntr.addComponent(num);
        cntr.addComponent(text);
        if (list.hasFocus() && isSelected) {
            int textWidth = text.getStyle().getFont().stringWidth(text.getText());
             text.setEndsWith3Points(false);
            //int numwidth = num.getStyle().getFont().stringWidth("5.  ");
         //  System.out.println("Text Width="+textWidth+"     "+)
            if (textWidth>list.getWidth()*0.75){//(list.getWidth()-numwidth)) {
                text.setShiftText(textPos);
               
            }
             if(textWidth+textPos<0)
            {
               textPos = textWidth;
            }
        } else {
            text.setShiftText(0);
            text.setEndsWith3Points(true);
        }
        if (isSelected) {
            cntr.setFocus(true);
            cntr.getStyle().setBgTransparency(100);
        } else {
            cntr.setFocus(false);
            cntr.getStyle().setBgTransparency(0);
        }

        return cntr;
    }
}

